package cc.grapevine.vps;

import cc.grapevine.vps.http.HttpManager;
import cc.grapevine.vps.http.HttpRequestVps;
import cc.grapevine.vps.http.HttpResponseVps;
import cc.grapevine.vps.http.URLManager;
import cc.grapevine.vps.view.ManagerButton;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class ManagerVps extends Activity
{
	private HttpManager m_httpManager;
	private MessageReciver m_messageReciver;

	private VpsData m_Data;

	private TextView m_Statues;

	private ProgressDialog pdDialog;

	private ImageView statusImageView;

	private TextView m_name;

	private ManagerButton start;

	private ManagerButton restart;

	private ManagerButton shutdown;

	private ManagerButton poweroff;

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.managervps);
		m_httpManager = HttpManager.getInstence();
		String m_Id = getIntent().getStringExtra(Intent.EXTRA_UID);
		String m_Name = getIntent().getStringExtra(Intent.EXTRA_TEXT);
		m_Data = UserManager.getIntence().getDataHashMap().get(m_Id + m_Name);
		init();
	}

	private void init()
	{

		poweroff = (ManagerButton) findViewById(R.id.manager_poweroff);
		poweroff.setResourceId(R.drawable.m_poweroff_btn, R.drawable.m_poweroff_3);

		start = (ManagerButton) findViewById(R.id.manager_start);
		start.setResourceId(R.drawable.m_boot_btn, R.drawable.m_boot_3);

		restart = (ManagerButton) findViewById(R.id.manager_restart);
		restart.setResourceId(R.drawable.m_reboot_btn, R.drawable.m_reboot_3);

		shutdown = (ManagerButton) findViewById(R.id.manager_shutdown);
		shutdown.setResourceId(R.drawable.m_shutdown_btn, R.drawable.m_shutdown_3);

		addListener(poweroff);
		addListener(start);
		addListener(restart);
		addListener(shutdown);

		m_Statues = (TextView) findViewById(R.id.status);
		statusImageView = (ImageView) findViewById(R.id.m_status_img);
		m_name = (TextView) findViewById(R.id.manager_name);
		setStatues();

		pdDialog = new ProgressDialog(this);
		pdDialog.setCancelable(false);
		pdDialog.setTitle(R.string.tip_title);
		pdDialog.setMessage(getString(R.string.tip_message));

		m_messageReciver = new MessageReciver();
	}

	private void setStatues()
	{

		if (Statues.SHUTDOWNING == m_Data.getStatues())
		{
			statusImageView.setImageResource(R.drawable.shutdowning);
			start.setEnabled(false);
			restart.setEnabled(false);
			shutdown.setEnabled(false);
			poweroff.setEnabled(true);
		}
		else if (Statues.RESTARTING == m_Data.getStatues())
		{
			statusImageView.setImageResource(R.drawable.restart);
			start.setEnabled(false);
			restart.setEnabled(true);
			shutdown.setEnabled(true);
			poweroff.setEnabled(true);
		}
		else if (Statues.STOPPED == m_Data.getStatues())
		{
			statusImageView.setImageResource(R.drawable.stopped);
			start.setEnabled(true);
			restart.setEnabled(false);
			shutdown.setEnabled(false);
			poweroff.setEnabled(false);
		}
		else if (Statues.INSTALLING == m_Data.getStatues())
		{
			start.setEnabled(false);
			restart.setEnabled(false);
			shutdown.setEnabled(false);
			poweroff.setEnabled(false);
			statusImageView.setImageResource(R.drawable.install_software);
		}
		else
		{
			start.setEnabled(false);
			restart.setEnabled(true);
			shutdown.setEnabled(true);
			poweroff.setEnabled(true);
			statusImageView.setImageResource(R.drawable.running);

		}
		m_name.setText(m_Data.getName());
		m_Statues.setText(m_Data.getStatues().name());
	}

	@Override
	protected void onStart()
	{

		setStatues();
		IntentFilter filter = new IntentFilter();
		filter.addAction(Action.UPDATE_STATUS);
		filter.addAction(Action.NO_LOGIN);
		filter.addAction(Action.HTTP_ERROR);
		filter.addAction(Action.COMMUNICATION_ERROR);
		filter.addAction(Action.MANAGER_POWEROFF);
		filter.addAction(Action.MANAGER_RESTART);
		filter.addAction(Action.MANAGER_SHUTDOWN);
		filter.addAction(Action.MANAGER_START);

		registerReceiver(m_messageReciver, filter);
		super.onStart();
	}

	@Override
	protected void onStop()
	{
		unregisterReceiver(m_messageReciver);
		super.onStop();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu)
	{
		menu.add(Menu.FIRST, Menu.FIRST, Menu.FIRST, R.string.refresh_statues).setIcon(R.drawable.ic_menu_refresh);
		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item)
	{
		boolean ret = super.onOptionsItemSelected(item);

		switch (item.getItemId())
		{
		case Menu.FIRST:
			HttpRequestVps requestVps = new HttpRequestVps();
			requestVps.setId(m_Data.getId());
			requestVps.setName(m_Data.getName());
			requestVps.setAction(Action.UPDATE_STATUS);
			requestVps.setUrl(URLManager.createDecStatus(m_Data.getId(), m_Data.getName()));
			if (null != m_httpManager)
				m_httpManager.addRequest(requestVps);
			pdDialog.show();
			break;

		default:
			break;
		}

		return ret;
	}

	private void addListener(View view)
	{

		view.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				final HttpRequestVps requestVps = new HttpRequestVps();

				requestVps.setName(m_Data.getName());
				requestVps.setId(m_Data.getId());

				switch (v.getId())
				{
				case R.id.manager_poweroff:
					new AlertDialog.Builder(ManagerVps.this).setTitle(R.string.tip_title).setMessage(
							R.string.tip_poweroff).setPositiveButton(R.string.ok, new DialogInterface.OnClickListener()
					{
						@Override
						public void onClick(DialogInterface dialog, int which)
						{
							requestVps.setUrl(URLManager.createPowerOffVps(m_Data.getId(), m_Data.getName()));
							requestVps.setAction(Action.MANAGER_POWEROFF);
							if (null != m_httpManager)
								m_httpManager.addRequest(requestVps);
							pdDialog.show();
						}
					}).setNegativeButton(R.string.cancel, null).create().show();
					break;

				case R.id.manager_restart:
					requestVps.setUrl(URLManager.createRestartVps(m_Data.getId(), m_Data.getName()));
					requestVps.setAction(Action.MANAGER_RESTART);
					if (null != m_httpManager)
						m_httpManager.addRequest(requestVps);
					pdDialog.show();
					break;

				case R.id.manager_shutdown:
					requestVps.setUrl(URLManager.createShutDownVps(m_Data.getId(), m_Data.getName()));
					requestVps.setAction(Action.MANAGER_SHUTDOWN);
					if (null != m_httpManager)
						m_httpManager.addRequest(requestVps);
					pdDialog.show();
					break;

				case R.id.manager_start:
					requestVps.setUrl(URLManager.createStartVps(m_Data.getId(), m_Data.getName()));
					requestVps.setAction(Action.MANAGER_START);
					if (null != m_httpManager)
						m_httpManager.addRequest(requestVps);
					pdDialog.show();
					break;
				default:
					break;
				}

				
			}
		});
	}

	@Override
	protected void onDestroy()
	{
		super.onDestroy();
	}

	private void showToast(int resId)
	{
		Toast.makeText(this, resId, Toast.LENGTH_SHORT).show();
	}

	private class MessageReciver extends BroadcastReceiver
	{
		@Override
		public void onReceive(Context context, Intent intent)
		{
			String action = intent.getAction();

			if (Action.UPDATE_STATUS.equals(action))
			{
				if (pdDialog.isShowing())
				{
					pdDialog.dismiss();
				}
				setStatues();
			}
			else if (Action.NO_LOGIN.equals(action))
			{
				if (pdDialog.isShowing())
				{
					pdDialog.dismiss();
				}
				showToast(R.string.nologin_error);
				Intent intent2 = new Intent(ManagerVps.this, Self2hostLogin.class);
				intent2.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
				intent2.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
				startActivity(intent2);
				ManagerVps.this.finish();
			}
			else if (Action.HTTP_ERROR.equals(action))
			{
				if (pdDialog.isShowing())
				{
					pdDialog.dismiss();
				}
				showToast(R.string.http_error);

			}
			else if (Action.COMMUNICATION_ERROR.equals(action))
			{
				if (pdDialog.isShowing())
				{
					pdDialog.dismiss();
				}
				showToast(R.string.comunication_error);
			}
			else
			{
				HttpResponseVps responseVps = (HttpResponseVps) intent.getExtras().getSerializable("data");
				HttpRequestVps requestVps = new HttpRequestVps();

				requestVps.setId(responseVps.vpsData.getId());
				requestVps.setName(responseVps.vpsData.getName());

				requestVps.setAction(Action.UPDATE_STATUS);
				requestVps.setUrl(URLManager
						.createDecStatus(responseVps.vpsData.getId(), responseVps.vpsData.getName()));
				if (null != m_httpManager)
					m_httpManager.addRequest(requestVps);
			}
		}
	}
}